// pages/user/user.js
import {
  USER_INFO,
} from '../../request/api.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    radar: [0, 0, 0, 0, 0, 0],
    userId:null,
    name:'',
    userInfo: {},
    medals:[],
    winHeight: 0,
    tabIndex: 0,
    fromShare: false,
    items:[
      {'count':0, 'name':'动弹'},
      {'count':0, 'name':'博客'},
      {'count':0, 'name':'关注'},
      {'count':0, 'name':'粉丝'},
    ],
    userTweetData:{
      type: 3,
      userId:''
    },
    blogData: {
      userId: '',
      isMyBlog: false,
    },
    followData: {
      type: 1,
      userId: '',
    },
    fansData: {
      type: 2,
      userId: '',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.userId || '';
    var name = options.name || '';
    var fromShare = (options.share || '0') == '1';
    var windowHeight = getApp().globalData.windowHeight - 30;
    var that = this;
    that.setData({
      userId: id,
      name: name,
      userInfo: {
        fromShare: fromShare,
      },
      winHeight: windowHeight,
      userTweetData:{
        type: 3,
        userId:id,
        height: windowHeight,
      },
      blogData: {
        userId: id,
        height: windowHeight,
      },
      followData: {
        userId: id,
        height: windowHeight,
      },
      fansData: {
        userId: id,
        height: windowHeight,
      },
    })
    this.getUserInfo(id, name);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var userId = that.data.userInfo.userId || '';
    var shareObj = {
      title: that.getShareTitle(), // 默认是小程序的名称(可以写slogan等)
      path: '/pages/user/user?userId=' + userId + '&share=' + 1, // 默认是当前页面，必须是以‘/’开头的完整路径
      // imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    return shareObj;
  },
  onShareTimeline: function(){
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: that.getShareTitle(), // 默认是小程序的名称(可以写slogan等)
      query: 'userId=' + that.data.id + '&share=' + 1, //当前页面路径携带的参数
      imageUrl: that.data.userInfo.portrait, //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      },
    };
    return shareObj;
  },

  getShareTitle: function(){
    var userInfo = getApp().globalData.userInfo;
    var userName;
    if(userInfo){
      userName = userInfo.name || '有人';
    } else {
      userName = '有人';
    }
    var title = userName + ' 分享了 ' + this.data.userInfo.name +' 的个人主页';
    return title;
  },


  /**
   * 截获竖向滑动
   */
  catchTouchMove:function(res){
    return false
  },

    /** 
   * 滑动切换tab 
   */
 bindChange: function(e) { 
  var that = this; 
  that.setData( { 
    tabIndex: e.detail.current 
  }); 
 }, 
 /** 
  * 点击tab切换 
  */
  clickTab: function (e) {
    const edata = e.currentTarget.dataset;
    this.setData({
     tabIndex: edata.tabindex,
    })
  },

  getUserInfo: function(id, name) {
    var param;
    if(id){
      param = {'id': id};
    } else {
      param = {'nickname': name};
    }
    USER_INFO(param).then(res =>{
      if(res.data.code == 1){
        var result = res.data.result;
        var id = result.id;
        var statistics = result.statistics;

        var radar = result.more.radar;
        var tempItems = this.data.items;
        tempItems[0].count = statistics.tweet;
        tempItems[1].count = statistics.blog;
        tempItems[2].count = statistics.follow;
        tempItems[3].count = statistics.fans;

        this.setData({
          id: id,
          userInfo: result,
          name: result.name,
          medals: result.more.medals,
          items: tempItems,
          radar: radar,
          reload: false,
          userTweetData:{
            type: 3,
            userId:id
          },
          blogData: {
            userId: id,
          },
          followData: {
            userId: id,
          },
          fansData: {
            userId: id,
          },
        })
      }
    }).catch(res => {

    })
  },
})